package me.firstapp.api.zerone;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;

import me.firstapp.api.base.BaseApi;
import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.JsonPage;
import me.firstapp.common.json.PageObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.ResponseUtils;
import me.firstapp.module.zerone.cqssc.CqsscLotteryResult;
import me.firstapp.module.zerone.cqssc.CqsscPlan;
import me.firstapp.service.zerone.cqssc.CqsscLotteryResultService;
import me.firstapp.service.zerone.cqssc.CqsscPlanService;

@Controller
@RequestMapping(value = "/zerone/cqsssc")
@Api(value = "/zerone/cqsssc", description = "重庆时时彩相关接口")
public class CqsscApi extends BaseApi {

	@Autowired
	private CqsscLotteryResultService cqsscLotteryResultService;

	@Autowired
	private CqsscPlanService cqsscPlanService;

	@RequestMapping(value = "findCqsscLotteryResultPage.do", method = RequestMethod.POST)
	@ApiOperation(value = "分页获取开奖结果", notes = "分页获取开奖结果")
	public void findCqsscLotteryResultPage(@RequestBody Map<String, Object> searchParams,
			@RequestParam(value = "pageNo", required = true, defaultValue = "1") Integer pageNo,
			@RequestParam(value = "pageSize", required = true, defaultValue = "20") Integer pageSize, String sortParam,
			String sortType, HttpServletRequest request, HttpServletResponse response) {
		PageObject<CqsscLotteryResult> result = new PageObject<>();
		try {
			Page<CqsscLotteryResult> resultPage = cqsscLotteryResultService.findPage(pageNo, pageSize, searchParams,
					sortParam, sortType);
			JsonPage<CqsscLotteryResult> jsonPage = new JsonPage<CqsscLotteryResult>();
			jsonPage.setList(resultPage.getEntities());
			jsonPage.setPageNo(resultPage.getPageNo());
			jsonPage.setPageSize(resultPage.getPageSize());
			jsonPage.setTotalCount(resultPage.getEntityCount());
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setPage(jsonPage);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "addCqsscLotteryResult.do", method = RequestMethod.POST)
	@ApiOperation(value = "新增开奖结果", notes = "新增开奖结果")
	public void addCqsscLotteryResult(String issueNo, String prizeNo, HttpServletRequest request,
			HttpServletResponse response) {
		SingleObject<CqsscLotteryResult> result = new SingleObject<CqsscLotteryResult>();
		try {
			String planIssueDate = issueNo.split("-")[0];
			int planIssueSubNum = Integer.valueOf(issueNo.split("-")[1]);

			CqsscLotteryResult cqsscLotteryResult = cqsscLotteryResultService.add(issueNo, prizeNo);

			if (planIssueSubNum < 8 || planIssueSubNum > 110) {
				CqsscPlan plan = new CqsscPlan();
				plan.setPlanIssueNo(issueNo);
				plan.setPlanIssueDate(planIssueDate);
				plan.setPlanIssueSubNum(planIssueSubNum);
				plan.setNotWinNum(5);
				plan.setPlanNo("不计划");
				plan.setWei("无");
				plan.setStatus(CqsscPlan.CQSSC_PLAN_STATUS_NOT_PLAN);
				plan.setTimesNo(0);
				plan.setOpenNo(prizeNo);
				cqsscPlanService.executeSave(plan);
			} else {
				// 中奖情况分析。如果这一期的非不计划的计划为空，则说明上一期的计划在进行倍投。
				List<CqsscPlan> cqsscPlans = cqsscPlanService.findPlanCqsscPlansByPlanIssueNo(issueNo);
				if (null == cqsscPlans || cqsscPlans.isEmpty()) {
					int prePlanIssueSubNum = planIssueSubNum - 1;
					String prePlanIssueNo = planIssueDate + "-";
					if (prePlanIssueSubNum < 10) {
						prePlanIssueNo = prePlanIssueNo + "00" + prePlanIssueSubNum;
					} else if (prePlanIssueSubNum > 9 && prePlanIssueSubNum < 100) {
						prePlanIssueNo = prePlanIssueNo + "0" + prePlanIssueSubNum;
					} else if (prePlanIssueSubNum > 99) {
						prePlanIssueNo = prePlanIssueNo + prePlanIssueSubNum;
					}
					cqsscPlanService.executeSetPrizeNo(prePlanIssueNo, prizeNo, true);
				} else {
					cqsscPlanService.executeSetPrizeNo(issueNo, prizeNo, false);
				}
				// 添加完成后就新增下一期的计划
				int nextPlanIssueSubNum = planIssueSubNum + 1;
				String nextPlanIssueNo = planIssueDate + "-";
				if (nextPlanIssueSubNum < 10) {
					nextPlanIssueNo = nextPlanIssueNo + "00" + nextPlanIssueSubNum;
				} else if (nextPlanIssueSubNum > 9 && nextPlanIssueSubNum < 100) {
					nextPlanIssueNo = nextPlanIssueNo + "0" + nextPlanIssueSubNum;
				} else if (nextPlanIssueSubNum > 99) {
					nextPlanIssueNo = nextPlanIssueNo + nextPlanIssueSubNum;
				}
				cqsscPlanService.addPlans(nextPlanIssueNo, 5);
			}

			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(cqsscLotteryResult);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "updateCqsscLotteryResult.do", method = RequestMethod.POST)
	@ApiOperation(value = "修改开奖结果", notes = "修改开奖结果")
	public void updateCqsscLotteryResult(Long id, String issueNo, String prizeNo, HttpServletRequest request,
			HttpServletResponse response) {
		SingleObject<CqsscLotteryResult> result = new SingleObject<CqsscLotteryResult>();
		try {
			CqsscLotteryResult cqsscLotteryResult = cqsscLotteryResultService.update(id, issueNo, prizeNo);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(cqsscLotteryResult);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "addPlans.do", method = RequestMethod.POST)
	@ApiOperation(value = "新增计划", notes = "新增计划")
	public void addPlans(String planIssueNo, int notWinNum, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Object> result = new SingleObject<Object>();
		try {
			cqsscPlanService.addPlans(planIssueNo, notWinNum);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "findCqsscPlanPage.do", method = RequestMethod.POST)
	@ApiOperation(value = "分页获取计划", notes = "分页获取计划")
	public void findCqsscPlanPage(@RequestBody Map<String, Object> searchParams,
			@RequestParam(value = "pageNo", required = true, defaultValue = "1") Integer pageNo,
			@RequestParam(value = "pageSize", required = true, defaultValue = "20") Integer pageSize, String sortParam,
			String sortType, HttpServletRequest request, HttpServletResponse response) {
		PageObject<CqsscPlan> result = new PageObject<>();
		try {
			Page<CqsscPlan> resultPage = cqsscPlanService.findPage(pageNo, pageSize, searchParams, sortParam, sortType);
			JsonPage<CqsscPlan> jsonPage = new JsonPage<CqsscPlan>();
			jsonPage.setList(resultPage.getEntities());
			jsonPage.setPageNo(resultPage.getPageNo());
			jsonPage.setPageSize(resultPage.getPageSize());
			jsonPage.setTotalCount(resultPage.getEntityCount());
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setPage(jsonPage);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "findCqsscLotteryResultById.do", method = RequestMethod.GET)
	@ApiOperation(value = "查询结果", notes = "查询结果")
	public void findCqsscLotteryResultById(Long id, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<CqsscLotteryResult> result = new SingleObject<CqsscLotteryResult>();
		try {
			CqsscLotteryResult cqsscLotteryResult = cqsscLotteryResultService.findById(id);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(cqsscLotteryResult);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}
}
